package com.kid.kidliving.commodity.service.impl;

import com.kid.kidliving.commodity.dao.AttrAttrgroupRelationDao;
import com.kid.kidliving.commodity.entity.AttrAttrgroupRelationEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kid.common.utils.PageUtils;
import com.kid.common.utils.Query;

import com.kid.kidliving.commodity.dao.AttrDao;
import com.kid.kidliving.commodity.entity.AttrEntity;
import com.kid.kidliving.commodity.service.AttrService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    //装配AttrAttrgroupRelationDao
    @Resource
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 可以完成保存商品基本属性的同时，还需要保存基本属性和属性组的关联关系
     *
     * 因为该方法涉及对两个表的insert操作，所以需要进行事物管理
     * @param attr
     */
    @Override
    @Transactional
    public void saveAttrAndRelation(AttrEntity attr) {
        //先保存基本数据
        this.save(attr);
        //进一步保存基本属性和属性组的关联关系
        if(attr.getAttrType()==1&& attr.getAttrGroupId()!=null){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(attr.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationDao.insert(attrAttrgroupRelationEntity);
        }
    }


    //    增加根据分类categoryId+查询条件key进行基本属性分页检索的api接口
    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long categoryId) {
        QueryWrapper<AttrEntity> attrType = new QueryWrapper<AttrEntity>().eq("attr_type", 1);

        if(categoryId!=0) {
            attrType.eq("category_id", categoryId);
        }

        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)) {
            attrType.and((obj)->{
                obj.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                attrType
        );
        return new PageUtils(page);
    }

    //    增加根据分类categoryId+查询条件key进行销售属性分页检索的api接口
    @Override
    public PageUtils querySaleAttrPage(Map<String, Object> params, Long categoryId) {

        QueryWrapper<AttrEntity> attrType = new QueryWrapper<AttrEntity>().eq("attr_type", 0);

        if(categoryId!=0) {
            attrType.eq("category_id", categoryId);
        }

        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)) {
            attrType.and((obj)->{
                obj.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                attrType
        );
        return new PageUtils(page);
    }

}